/**
 * Created with JetBrains WebStorm.
 * User: zhoums
 * Date: 14-4-21
 * Time: 上午11:46
 * To change this template use File | Settings | File Templates.
 */

var $contentWarp = $(".mark_item");

$(function () {
    var _this = this;
    var oContent = getWeibo_CON();
    if (oContent.status === "success" && oContent.data.length > 0) {
        renderWeibo(oContent.data);
    }
    /*
     * 发布按钮事件 #issueWb_btn
     * */
    $("#issueWb_btn").on("click", function () {
//        todo 添加图片功能。
        var sContent = $("#daymark_input").val();
        if (sContent != "" || sContent.length > 0) {
            issueWeibo(sContent);
        } else {
            var p = $('.input_area');
            toggleAlarm(p)
        }
    })
})
/*
 * 发布WEIBO
 * @sContent{String} 内容正文
 * @sPhotoUrl{String} 附件图片URL
 * */
function issueWeibo(sContent, sPhotoUrl) {
    var _content = sContent ? sContent : "", _phpto = sPhotoUrl ? sPhotoUrl : "", param = $.parseJSON('{"content":"' + _content + '","phpto":"' + _phpto + '"}');
    $.ajax({
        url: fm.config.buildPath + 'account/backEnd/issueWeibo.php',
        type: 'post',
        data: param,
        success: function (data) {
            var oData = $.parseJSON(data),_time=new Date();
            param=$.extend(param,{issueTime:_time.format("yyyy-MM-dd hh:mm:ss")})
            renderWeibo([param]);
//                if(oData.)
        }
    })
}
/*
 * fn renderWeibo : 渲染 weiBo 内容
 * @ aData{array[]} 内容数组
 * @ isClean{boolean} true:先清除内容 false:不清除内容
 * */
function renderWeibo(aData, isClean) {
    if ((typeof isClean == "undefined")) {
        isClean = true;
    }
    isClean ? cleanWeibo_con() : "";
    $.each(aData, function (i, item) {
//        console.log(item);
        var _html, _photo = "", _content = '<div class="item_nav">' +
            '<img class="faceImg" src=""> ' +
            '<div class="item_cont">' +
            '<p>' + item.content + '</p> ' +
            '</div>',nReply=item.replys,replyNum="";
        if (item.photo) {
            _photo = '<div class="item_photo bigImg">' +
                '<img src="img/Photo2.jpg" width="100"> ' +
                '</div>'
        }
        if(nReply>0){
            replyNum="("+nReply+")";
        }
        _html = _content + _photo + '<div class="item_reply"><span class="issueTime">' + item.issueTime + '</span><span class="item_statu"><a class="likeTi"></a><a class="replyTi" href="javascript:;">评论'+replyNum+'</a></span></div>' +
            ' <div class="cls"></div>' +
            ' </div>';
        $contentWarp.prepend(_html);
    })
}
/*
 * fn getWeibo_CON : 获取 WEIBO 数据
 * @ Return  jObject{json OBJ}
 * */
function getWeibo_CON() {
    var _url = fm.config.buildPath + 'account/backEnd/fetchWeibo.php', jObject;
    $.ajax({
        url: _url,
        type: 'get',
        cache: false,
        async: false,
        success: function (data) {
            var _data = data.replace(/\\"/g, '"');
            jObject = $.parseJSON(_data);
        }
    })
    return jObject;
}

/*
 * fn cleanWeibo_con : 清空 weiBo 内容
 * */
function cleanWeibo_con() {
    $contentWarp.empty();
}


/*
* fn comment: 添加评论
* content｛string｝评论内容
* author{string}评论人
* weiboId{number}被评论WeiBo 的 ID
* */
function comment(content,author,weiboId){
    var param='{"content":'+content+',"author":'+author+',"id":'+weiboId+'}' ;
    $.ajax({
        url:fm.config.buildPath+"",
        type:'post',
        data: $.parseJSON(param),
        success:function(data){
            console.log(data);
        }
    })


}